<script setup>
import { ref } from 'vue'
import { ArticleAddChannelService, ArticleEditChannelService } from '@/api/article.js'

const dialogVisible = ref(false)
const formRef = ref()

//组件对外暴露方法，外部控制添加还是编辑
const openEditWay = (row) => {
  dialogVisible.value = true
  fromModel.value = { ...row }
}
defineExpose({
  openEditWay,
})
const fromModel = ref({
  cate_name: '',
  cate_alias: '',
})
const fromRules = {
  cate_name: [
    { require: true, message: '请输入', trigger: 'blur' },
    { pattren: /^\S{1,10}$/, message: '必须是1~10位非空字符', trigger: 'blur' },
  ],
  cate_alias: [
    { require: true, message: '请输入', trigger: 'blur' },
    { pattren: /^\S{1,10}$/, message: '必须是1~10位非空字符', trigger: 'blur' },
  ],
}
const onSubmit = async () => {
  await formRef.value.validate()
  !fromModel.value.id
    ? ArticleAddChannelService(fromModel.value)
    : ArticleEditChannelService(fromModel.value)
  dialogVisible.value = false
}
</script>
<template>
  <el-dialog
    v-model="dialogVisible"
    :title="fromModel.id ? 添加分类 : 编辑分类"
    width="30%"
    :before-close="handleClose"
  >
    <el-form :model="fromModel" :rules="fromRules" ref="formRef">
      <el-form-item label="分类名称" prop="cate_name">
        <el-input placeholder="请输入分类名称" v-model="fromModel.cate_name"></el-input>
      </el-form-item>
      <el-form-item label="分类别名" prop="cate_alias">
        <el-input placeholder="请输入分类别名" v-model="fromModel.cate_alias"></el-input>
      </el-form-item>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="onSubmit"> 确认 </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style scoped></style>
